package com.AAL.Action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.AAL.DAO.AccountsDAO;
import com.AAL.DAO.DealerDAO;
import com.AAL.DAO.StatusOfAccountDAO;
import com.AAL.Model.*;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class DealerAction extends ActionSupport implements SessionAware {

	/*
	 * Khai bao cac thuoc tinh
	 */
	
	private Account myAccount;
	private AccountsDAO lnkAccountsDAO;

	private Map<String, Object> session;
	private DealerDAO lnkDealer;
	private AccountsDAO lnkAccount;
	private StatusOfAccountDAO lnkStatusOfAccount;
	private List<Dealer> lstDealer;
	private Dealer dealer;
	
	private List<StatusOfAccount> lstStatusOfAccounts;
	private StatusOfAccountDAO lnkStatus;
	
	public DealerAction(){
		this.lnkDealer = new DealerDAO();
		this.lnkStatusOfAccount = new StatusOfAccountDAO();		
		this.lnkAccountsDAO = new AccountsDAO();
	}
	
	
	/*
	 * Minh
	 */
	public String indexForDealer() {
		try {
			
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;		
	}
	public String viewProfileForDealer() {
		try {
			myAccount = (Account) session.get("myAccount");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
		
	}
	public String editProfileForDealer() {
		try {
			System.out.println("goi ham edit");
			myAccount = (Account) session.get("myAccount");
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
		
	}
	
	public String finishEditProfileForDealer() {
		try {
			System.out.println("goi ham ket thuc edit");
			myAccount = lnkAccountsDAO.saveAndUpdateAccount(myAccount);
			if(myAccount==null){
				addActionError("Lỗi: Không thể cập nhật thông tin của đại lý.");
				return ERROR;
			}
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	
	public String changePasswordForDealer() {
		try {
			
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
		
	}
	
	public String finishChangePasswordForDealer() {
		HttpServletRequest request = (HttpServletRequest) ActionContext
		.getContext().get(ServletActionContext.HTTP_REQUEST);
		try {
			String oldPassword = request.getParameter("oldPassword");
			String newPassword = request.getParameter("newPassword");				
			if(session.get("myAccount")!=null)
				myAccount = (Account) session.get("myAccount");
			if(myAccount.getPassword().equals(oldPassword)){
				myAccount.setPassword(newPassword);
				myAccount = lnkAccountsDAO.changePassword(myAccount);
				
				if(myAccount==null){
					addActionError("Không thể thay đổi mật khẩu.");
					return ERROR;
				}
			}else {
				addActionError("Bạn nhập sai mật khẩu cũ.");
				return ERROR;
			}			
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
		
	}
	/*
	 * Ket thuc Minh
	 */
	

	/*
	 * Huyen
	 */
	
	
	//thu 4 ngay 30/3/2011
	//Huyen
	//Lay ta ca cac delaer va trang thai khi trang quan ly dealer duoc load len
	public String Load_First_Time() {
		
		System.out.println("Goi ham load first time");	
		this.lstStatusOfAccounts = lnkStatusOfAccount.getAllStatusOfAccounts();
		this.lstDealer = lnkDealer.getAllLstDealer();	
		System.out.println("thuc hien xong ham ");
		return SUCCESS;
	}
	
	public String dealerManagement() {
		try {
			
		} catch (Exception e) {
			return ERROR;
		}
		return SUCCESS;
	}
	
	
	@Override
	public void setSession(Map<String, Object> arg0) {
		this.session = arg0;
	}
	public Map<String, Object> getSession() {
		return session;
	}

	//thu 2: 28/3/2011
	//Huyen
	//Lay thong tin cua 1 dealer theo ID duoc truyen vao
	public String GetDealerById() {
		System.out.println("view chi tiet");
		try {
			HttpServletRequest request = (HttpServletRequest) ActionContext
			.getContext().get(ServletActionContext.HTTP_REQUEST);
			int Id=(Integer.parseInt(request.getParameter("id")));
			lnkDealer=new DealerDAO();
			dealer=lnkDealer.getDealer(Id);			
		} catch (Exception e) {			
			addActionError("Error:"+ e.getMessage());
			return ERROR;
		}
		return SUCCESS;
	}
	
	//thu 2: 28/3/2011
	//Huyen
	//Kich hoat 1 dealer
	public String KichHoat() {
		try {
			HttpServletRequest request = (HttpServletRequest) ActionContext
			.getContext().get(ServletActionContext.HTTP_REQUEST);
			int Id=(Integer.parseInt(request.getParameter("id")));
			lnkDealer=new DealerDAO();
			dealer=lnkDealer.getDealer(Id);	
		/*	int accountID=dealer.getAccount().getAccountID();*/
			lnkAccount=new AccountsDAO();
			lnkStatus = new StatusOfAccountDAO();
			this.lstStatusOfAccounts = lnkStatus.getAllStatusOfAccounts();
			/*	lnkAccount.KichHoat(accountID);*/
			dealer.getAccount().setStatusOfAccount(this.lstStatusOfAccounts.get(0));
			lnkAccount.saveAndUpdateAccount(dealer.getAccount());
		} catch (Exception e) {			
			return ERROR;
		}
		return SUCCESS;
	}
	
	//thu 3: 29/3/2011
	//Huyen
	//Tạm khóa 1 dealer
	public String TamKhoa() {
		try {
			HttpServletRequest request = (HttpServletRequest) ActionContext
			.getContext().get(ServletActionContext.HTTP_REQUEST);
			int Id=(Integer.parseInt(request.getParameter("id")));
			lnkDealer=new DealerDAO();
			dealer=lnkDealer.getDealer(Id);	
		/*	int accountID=dealer.getAccount().getAccountID();*/
			lnkAccount=new AccountsDAO();
			lnkStatus = new StatusOfAccountDAO();
			this.lstStatusOfAccounts = lnkStatus.getAllStatusOfAccounts();
			/*	lnkAccount.KichHoat(accountID);*/
			dealer.getAccount().setStatusOfAccount(this.lstStatusOfAccounts.get(2));
			lnkAccount.saveAndUpdateAccount(dealer.getAccount());
		} catch (Exception e) {			
			return ERROR;
		}
		return SUCCESS;
	}
	
	//thu 3: 29/3/2011
	//Huyen
	//Huy 1 dealer
	public String Huy() {
		try {
			HttpServletRequest request = (HttpServletRequest) ActionContext
			.getContext().get(ServletActionContext.HTTP_REQUEST);
			int Id=(Integer.parseInt(request.getParameter("id")));
			lnkDealer=new DealerDAO();	
			lnkDealer.deleteDealer(Id);	
		} catch (Exception e) {			
			return ERROR;
		}
		return SUCCESS;
	}	
	
	/*
	 * Ket thuc Huyen
	 */
	
	
	/*
	 * Khai bao cac phuong thuc get set
	 */
	
	/*
	 * Thuong
	 */
	
	/*
	 * Ket thuc Thuong
	 */
	public List<Dealer> getLstDealer() {
		return lstDealer;
	}
	public void setLstDealer(List<Dealer> lstDealer) {
		this.lstDealer = lstDealer;
	}
	public Dealer getDealer() {
		return dealer;
	}
	public void setDealer(Dealer dealer) {
		this.dealer = dealer;
	}

	public List<StatusOfAccount> getLstStatusOfAccounts() {
		return lstStatusOfAccounts;
	}

	public void setLstStatusOfAccounts(List<StatusOfAccount> lstStatusOfAccounts) {
		this.lstStatusOfAccounts = lstStatusOfAccounts;
	}


	public Account getMyAccount() {
		return myAccount;
	}


	public void setMyAccount(Account myAccount) {
		this.myAccount = myAccount;
	}
}
